package com.easidea.tim.flex.remote.purchase
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.purchase.PurchaseReturn;
	import com.easidea.tim.flex.bean.purchase.PurchaseReturnLocator;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	

	public class PurchaseReturnDao extends BaseDao
	{
		
		private var _purchaseReturnService:RemoteObject = new RemoteObject();
		[Bindable]private var purchaseReturnLocator:PurchaseReturnLocator = PurchaseReturnLocator.getInstance();
		private static var _instance:PurchaseReturnDao;
		private var _orderId:int;
		private var _status:String;
		
		public static function getInstance():PurchaseReturnDao{
			if( !_instance ){
				_instance = new PurchaseReturnDao();
				
			}
			return _instance;
		}
		public function PurchaseReturnDao() {
				_purchaseReturnService.channelSet = this.buildChannelSet();
				_purchaseReturnService.destination="purchaseReturnService";
				_purchaseReturnService.concurrency = "last";
				_purchaseReturnService.makeObjectsBindable = true;	
				_purchaseReturnService.showBusyCursor = true;
		}		
		
				/**
		 * 调用spring方法:分页查询采购合同列表
		 * @param name
		 * @param page
		 * 
		 */		
		public function getPurchaseReturnList(creator:String,saleOrderNum:String, customerName:String,startTime:Date,endTime:Date,page:Page,status:String):void {			
			_purchaseReturnService.addEventListener(ResultEvent.RESULT, showPurchaseReturnList);
			_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
			_purchaseReturnService.getPurchaseReturnListByCondition(creator,saleOrderNum,customerName,startTime,endTime,page,status);
		}
		
		public function getPurchaseReturnListByCreator(creator:String,saleOrderNum:String, customerName:String,startTime:Date,endTime:Date,page:Page,status:String):void {			
			_purchaseReturnService.addEventListener(ResultEvent.RESULT, showPurchaseReturnList);
			_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
			_purchaseReturnService.getPurchaseReturnListByCreator(creator,saleOrderNum,customerName,startTime,endTime,page,status);
		}
		
		public function getPurchaseReturnById(id:String):void{
			_purchaseReturnService.addEventListener(ResultEvent.RESULT, showPurchaseReturn);
			_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
			_purchaseReturnService.GetPurchaseReturnByid(id);
		}
		
		private function showPurchaseReturn(e:ResultEvent):void {	 
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, showPurchaseReturn);
			 if(e.result != null) {	
				var purchaseReturn:PurchaseReturn = e.result as PurchaseReturn;					 				 
				 Env.dispatchEvent(new CustomerEvent("getSPurchaseReturnDetail",purchaseReturn));				  									 
			 } 
		}
		/**
		 * 分页查询销售订单列表回调函数
		 * @param e
		 * 
		 */			
		private function showPurchaseReturnList(e:ResultEvent):void {	
					 
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, showPurchaseReturnList);
			 if(e.result != null) {		
				var page:Page = e.result as Page;					 				 
				 Env.dispatchEvent(new CustomerEvent("getPurchaseReturnListSuccess",page));				  									 
			 } 
		}
			/**
		 * 调用spring方法:新增或修改销售订单需要的初始化数据
		 * @param name
		 * @param page
		 * 
		 */	
		public function getInitDataList(id:int):void {
			_orderId = id;
			_purchaseReturnService.addEventListener(ResultEvent.RESULT, showInitDataList);
			_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
			_purchaseReturnService.getInitDataList();
			
		}

		
		private function showInitDataList(e:ResultEvent):void {			 
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, showInitDataList);			
			 var result:ArrayCollection = e.result as ArrayCollection; 	 
			 if(result != null ) {		
					Env.dispatchEvent(new CustomerEvent("getPurchaseReturnSuccess",result));
				}
		}

		
		/**
		 *调用spring方法:保存销售订单和明细列表 
		 * @param customer
		 * 
		 */		
		public function savePurchaseReturn(order:PurchaseReturn,items:ArrayCollection,status:String):void {
				_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseReturnService.addEventListener(ResultEvent.RESULT, savePurchaseReturnSuccess);
				_status = status;
				_purchaseReturnService.savePurchaseReturn(order,items);
		}
		
		/**
		 *调用spring方法:保存销售订单和明细列表 
		 * @param customer
		 * 
		 */		
		public function updatePurchaseReturn(order:PurchaseReturn,items:ArrayCollection,status:String):void {
				_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseReturnService.addEventListener(ResultEvent.RESULT, savePurchaseReturnSuccess);
				_status = status;
				_purchaseReturnService.updatePurchaseReturn(order,items);
		}
	   /**
	    *保存销售订单和明细列表成功回调函数 
	    * @param e
	    * 
	    */		
	   private function savePurchaseReturnSuccess(e:ResultEvent):void {			 
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, savePurchaseReturnSuccess);
			 if(e.result != null) {
				AppAlert.show("保存成功！","提示");
				getPurchaseReturnList("","","",null,null,new Page(),_status);
				Env.dispatchEvent(new CustomerEvent("savePurchaseReturnSuccess"));
			 } 
		}
		
		public function completeOrder(order:PurchaseReturn,items:ArrayCollection):void {
				_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseReturnService.addEventListener(ResultEvent.RESULT, completePurchaseReturnSuccess);
				_purchaseReturnService.CompleteUpdate(order,items);
		}
	   /**
	    *保存销售订单和明细列表成功回调函数 
	    * @param e
	    * 
	    */		
	   private function completePurchaseReturnSuccess(e:ResultEvent):void {			 
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, completePurchaseReturnSuccess);
			 if(e.result != null) {
				AppAlert.show("填充成功！","提示");
				getPurchaseReturnList("","","",null,null,new Page(),"0");
				Env.dispatchEvent(new CustomerEvent("completePurchaseReturnSuccess"));
			 } 
		}
		/**
		 *调用spring方法:删除销售订单
		 * @param customer
		 * 
		 */		
		public function deletePurchaseReturn(id:String):void {
				_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseReturnService.addEventListener(ResultEvent.RESULT, deletePurchaseReturnSuccess);
				_purchaseReturnService.deletePurchaseReturn(id);
		}
	   /**
	    * 删除销售订单成功回调函数 
	    * @param e
	    * 
	    */		
	   private function deletePurchaseReturnSuccess(e:ResultEvent):void {			 
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, deletePurchaseReturnSuccess);
			 if(e.result != null&& e.result == "success") {
				AppAlert.show("删除成功！","提示");
				Env.dispatchEvent(new  CustomerEvent("reGetPurchaseReturnList"));
			 } 
		}
		
		public function JudgeisExistProduct(productName:String,color:String):void{
			_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
			_purchaseReturnService.addEventListener(ResultEvent.RESULT, isExistProductSuccess);
			_purchaseReturnService.isExistProduct(productName,color);
		}
		
		private function isExistProductSuccess(e:ResultEvent):void{
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, isExistProductSuccess);
			Env.dispatchEvent(new  CustomerEvent("isExistProduct",e.result));
			
		}
		
		
		/**
		 * 
		public function JudgeisCompleteProduct(productName:String,color:String,flowNo:String,meter:String):void{
			_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
			_purchaseReturnService.addEventListener(ResultEvent.RESULT, isCompleteProductSuccess);
			_purchaseReturnService.isCompleteProduct(productName,color,flowNo,meter);
		}
		
		private function isCompleteProductSuccess(e:ResultEvent):void{
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, isCompleteProductSuccess);
			Env.dispatchEvent(new  CustomerEvent("isCompleteProduct",e.result));
			
		}
		
		/**
		 *调用spring方法：更新订单状态
		 * @param id
		 * @param closeMan
		 * 
		 */		
		public  function updatePurchaseReturnStatus(id:int, status:int,responser:String):void {
				_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseReturnService.addEventListener(ResultEvent.RESULT, updatePurchaseReturnStatusSuccess);
				_purchaseReturnService.updatePurchaseReturnStatus(id,status,responser);				
		}
		
		public  function updatePurchaseReturnSend(id:int,responser:String):void {
				_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseReturnService.addEventListener(ResultEvent.RESULT, updatePurchaseReturnStatusSuccess);
				_purchaseReturnService.updatePurchaseReturnSend(id,responser);				
		}
	  /**
	    * 合同关闭回调函数 
	    * @param e
	    * 
	    */		
	   private function updatePurchaseReturnStatusSuccess(e:ResultEvent):void {			 
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, updatePurchaseReturnStatusSuccess);
			 if(e.result != null && e.result == "success") {
				AppAlert.show("销售订单状态已更改！","提示");
			    Env.dispatchEvent(new  CustomerEvent("reGetPurchaseReturnList"));
			 } 
		}
		/**
		 * 
		 * 调用spring方法：获取产品信息
		 */		
		public  function getAllProducts():void {
				_purchaseReturnService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseReturnService.addEventListener(ResultEvent.RESULT,getAllProductsSuccess);
				_purchaseReturnService.getAllProducts();				
		}
	   /**
	    * 取产品信息成功回调函数 
	    * @param e
	    * 
	    */		
	   private function getAllProductsSuccess(e:ResultEvent):void {			 
			_purchaseReturnService.removeEventListener(ResultEvent.RESULT, getAllProductsSuccess);
			 if(e.result != null) {
			   	Env.dispatchEvent(new  CustomerEvent("getAllProductsSuccess",e.result));					    
			 } 
		}

		public function updateMemo(saleorder:PurchaseReturn,memo:String):void{
			_purchaseReturnService.UpdateMemo(saleorder,memo);		
		}
	}
}